//
//  AddBankCardViewController.m
//  HuaMuJinRong
//
//  Created by 吴建东 on 16/4/29.
//  Copyright © 2016年 EIMS. All rights reserved.
//

#import "AddBankCardViewController.h"
#import "AccountItem.h"
#import "payLimitViewController.h"
#import "MyBankCardViewController.h"
#import "RealNameAuthView.h"
#import "LimitTableViewController.h"
#define kSelectViewHeight 200.0
@interface AddBankCardViewController ()<UIScrollViewDelegate,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource,HTTPClientDelegate,UIAlertViewDelegate,LimitTableViewControllerDelegate>
{
    UIView *_alertView;
    UIView *_alertBackView;
    
    NSInteger _selectViewType; /** * 1银行 2省市  */
    
    NSInteger _locationSelectIndex;
    NSInteger _bankNameSelectIndex;
    
    NSMutableArray *_citysArr;//城市数组
    NSMutableArray *_cityIdArr;//城市ID数组
    NSArray *_cityNameArr;
    
    NSMutableArray *_provinceDataArray;
    NSMutableArray *_bankDataArray;
    
    NSString *_holderBankIdStr;
    NSString *_holderProvIdStr;
    NSString *_holderCityIdStr;
    
    NSString *_bankIdStr;
    NSString *_bankNameStr;
    NSString *_provinceIdStr;
    NSString *_cityIdStr;
    NSString *_provinceStr;
    NSString *_screenStr;
    NSMutableDictionary *_cityDics;
}
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UILabel *usernameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *headerImageView;
@property (weak, nonatomic) IBOutlet UILabel     *headerOneValue;
@property (weak, nonatomic) IBOutlet UILabel     *headerTwoValue;
@property (weak, nonatomic) IBOutlet UILabel     *headerThreeValue;
@property (weak, nonatomic) IBOutlet UILabel     *realNameValue;
@property (weak, nonatomic) IBOutlet UILabel     *bankNameValue;
@property (weak, nonatomic) IBOutlet UITextField *bankNameTextField;
@property (weak, nonatomic) IBOutlet UILabel     *locationValue;
@property (weak, nonatomic) IBOutlet UITextField *locationTextField;
@property (weak, nonatomic) IBOutlet UILabel     *subBranchValue;
@property (weak, nonatomic) IBOutlet UITextField *subBranchTextField;
@property (weak, nonatomic) IBOutlet UITextField *realNameTextField;
@property (weak, nonatomic) IBOutlet UILabel     *idNumValue;
@property (weak, nonatomic) IBOutlet UITextField *idNumTextfield;
@property (weak, nonatomic) IBOutlet UILabel     *bankNumValue;
@property (weak, nonatomic) IBOutlet UITextField *bankNumTextField;
@property (weak, nonatomic) IBOutlet UILabel     *mobileValue;
@property (weak, nonatomic) IBOutlet UITextField *mobileTextField;
@property (weak, nonatomic) IBOutlet ClickButton *nextButtton;
@property (weak, nonatomic) IBOutlet UILabel *verifyNumValue;
@property (weak, nonatomic) IBOutlet UITextField *verifyNumTextField;
@property (weak, nonatomic) IBOutlet ClickButton *verifyNumButton;

@property(nonatomic ,strong) UIPickerView *pickerView;
@property(nonatomic ,strong) UIPickerView *bankPickerView;
@property(nonatomic ,strong) UILabel *provinceCityLabel;
@property(nonatomic ,strong) UILabel *bankLabel;
@property(nonatomic ,strong) UIView  *provinceBackView;
@property(nonatomic ,strong) UIView  *bankBackView;
@property(nonatomic ,strong) RealNameAuthView *realNameView;
@property (nonatomic, strong) NetWorkClient  *requestClient;
@property (nonatomic, assign) BOOL           isLoading;
@property (nonatomic, assign) NSInteger      requestType;

@end

@implementation AddBankCardViewController
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (_requestClient != nil) {
        [_requestClient cancel];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
   
    self.view.backgroundColor = KbackgroundColor;
    [self initNavBarTitle:@"实名认证支付" andLeftItemName:@"返回" andRightItemName:@"支持银行"];
    
    [self initData];
    
    [self initView];
    [self initSelectView];
    [self initAlertView];
    [self.realNameView showView];
}

#pragma mark - LimitTableViewControllerDelegate
- (void)cellSelectWithBankName:(NSString *)bankName bankCode:(NSString *)bankCode {
    _bankNameStr = bankName;
    _bankIdStr = bankCode;
    
    _bankNameTextField.text = _bankNameStr;
}

#pragma mark - Action
- (IBAction)nextButtonClick:(id)sender {
    //[self alertViewShow];
    if (_realNameTextField.text.length <= 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入您的真实姓名"];
        return;
    }
    if (_idNumTextfield.text.length <= 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入您的身份证号"];
        return;
    }
    if (_bankNumTextField.text.length <= 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入银行卡号"];
        return;
    }
    if (_bankNameTextField.text.length <= 0) {
        [SVProgressHUD showErrorWithStatus:@"请选择开户银行"];
        return;
    }
//    if (_locationTextField.text.length <= 0) {
//        [SVProgressHUD showErrorWithStatus:@"请选择开户省市"];
//        return;
//    }
//    if (_subBranchTextField.text.length <= 0) {
//        [SVProgressHUD showErrorWithStatus:@"请输入支行信息"];
//        return;
//    }
    if (_mobileTextField.text.length <= 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入银行预留手机号"];
        return;
    }
    if (_verifyNumTextField.text.length <= 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入验证码"];
        return;
    }
    
    [self submitBankInfoRequest];
}

- (IBAction)getVerifyNumButtonClick:(id)sender {
    if (![_mobileTextField.text isPhone]) {
        [SVProgressHUD showImage:nil status:@"手机号码格式不正确！"];
        return;
    }
    
    [self getVerifyCodeRequest];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag==15 && buttonIndex==0) {
        //实名认证信息
        MyBankCardViewController *myBankCard = [[MyBankCardViewController alloc] init];
        myBankCard.isComeAdd = YES;
        [self.navigationController pushViewController:myBankCard animated:YES];
    }
}

- (void)alertViewShow {
    _alertView.hidden = NO;
}

- (void)alertViewHidden {
    _alertView.hidden = YES;
}

- (void)successButtonClick {
    [self alertViewHidden];
}

- (void)failureButtonClick {
    [self alertViewHidden];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField == _bankNameTextField) {
        //            _selectViewType = 1;
        //            if (_bankNameStr.length <= 0) {
        //                _bankNameStr = _bankDataArray[0][@"name"];
        //                _holderBankIdStr = [NSString stringWithFormat:@"%@",_bankDataArray[0][@"code"]];
        //            }
        //            [_pickerView reloadAllComponents];
        
        LimitTableViewController *limitTable = [[LimitTableViewController alloc] init];
        limitTable.delegate = self;
        [self.navigationController pushViewController:limitTable animated:YES];
        
        return NO;
        [self selectViewHidden];
    }else if (textField == _locationTextField) {
        
        _selectViewType = 2;
        [_pickerView reloadAllComponents];
        
        if (_provinceStr.length <= 0) {
            if(_cityDics.count && _provinceDataArray.count) {
                
                NSInteger selectedProvIndex = 0;
                NSString *seletedProvince = _provinceDataArray[selectedProvIndex][@"name"];
                _holderProvIdStr = [NSString stringWithFormat:@"%@",_provinceDataArray[selectedProvIndex][@"code"]];
                _cityNameArr = [[_cityDics objectForKey:[NSString stringWithFormat:@"%@",_provinceDataArray[selectedProvIndex][@"code"]]] allValues];
                NSString *seletedCity = @"";
                if (_cityNameArr.count) {
                    [_citysArr removeAllObjects];
                    [_cityIdArr removeAllObjects];
                    for (AccountItem *cityItem in _cityNameArr) {
                        [_citysArr addObject:cityItem.name];
                        [_cityIdArr addObject:cityItem.rowId];
                    }
                    [_pickerView reloadComponent:1];
                }
                if (selectedProvIndex > _citysArr.count) {
                    
                    seletedCity = [_citysArr objectAtIndex:_citysArr.count -1];
                    _holderCityIdStr = _cityIdArr[_citysArr.count -1];
                }else{
                    seletedCity = [_citysArr objectAtIndex:selectedProvIndex];
                    _holderCityIdStr = _cityIdArr[selectedProvIndex];
                }
                _provinceStr = [NSString stringWithFormat:@"%@,%@", seletedProvince,seletedCity];
            }
        }

        [self selectViewShow];
        return NO;
    }else {
        [self selectViewHidden];
    }
    
    return YES;
}

#pragma 取消按钮
- (void)cancelClick {
    [UIView animateWithDuration:0.3 animations:^{
        [self selectViewHidden];
    } completion:^(BOOL finished) {
        
    }];
}

#pragma 确定按钮
- (void)sureClick {
    
    [UIView animateWithDuration:0.3 animations:^{
        [self selectViewHidden];
    } completion:^(BOOL finished) {
        if (_selectViewType == 1) {
            _bankNameTextField.text = _bankNameStr;
            _bankIdStr = _holderBankIdStr;
        }else {
            _locationTextField.text = _provinceStr;
            _provinceIdStr = _holderProvIdStr;
            _cityIdStr = _holderCityIdStr;
        }
    }];
}

- (void)selectViewShow {
    [self keyBoardHidden];
    
    _provinceBackView.frame = CGRectMake(0, MSHIGHT - 64 - kSelectViewHeight, MSWIDTH, kSelectViewHeight);
    [self.view bringSubviewToFront:_provinceBackView];
}

- (void)selectViewHidden {
    _provinceBackView.frame = CGRectMake(0, MSHIGHT + kSelectViewHeight, MSWIDTH, kSelectViewHeight);
}

- (void)keyBoardHidden {
    [_realNameTextField resignFirstResponder];
    [_idNumTextfield resignFirstResponder];
    [_bankNumTextField resignFirstResponder];
    [_bankNameTextField resignFirstResponder];
    [_locationTextField resignFirstResponder];
    [_subBranchTextField resignFirstResponder];
    [_mobileTextField resignFirstResponder];
}

- (void)changeValue {
    if (_realNameTextField.text.length>0 && _idNumTextfield.text.length>0 && _bankNameTextField.text.length>0 && _bankNumTextField.text.length>0 && _mobileTextField.text.length>0 && _verifyNumTextField.text.length>0) { // && _locationTextField.text.length>0 && _subBranchTextField.text.length>0
        [CommonClass buttonEnableWithButton:_nextButtton enable:YES];
    }else {
        [CommonClass buttonEnableWithButton:_nextButtton enable:NO];
    }
}

#pragma mark - 请求数据
- (void)initData {
    if (_bankCard == nil) {
        _bankCard = [[BankCardModel alloc] init];
    }
    _selectViewType = 1;
    _bankNameSelectIndex = 0;
    _locationSelectIndex = 0;
    
    _bankIdStr = @"";
    _provinceIdStr = @"";
    _cityIdStr = @"";
    _provinceStr = @"";
    _screenStr = @"2";
    _bankDataArray = [NSMutableArray array];
    _provinceDataArray = [NSMutableArray array];
    _cityIdArr = [NSMutableArray array];
    _cityDics = [NSMutableDictionary dictionary];
    _citysArr = [NSMutableArray array];
    _cityNameArr = @[];
    
//    _realNameTextField.text = AppDelegateInstance.userInfo.realName;
//    _idNumTextfield.text = AppDelegateInstance.userInfo.idNo;
    
    [self requestData];
}

- (void)getVerifyCodeRequest {
    if (_isLoading) {
        [SVProgressHUD showImage:nil status:@"正在请求，请稍候再试..."];
        return;
    }
    _requestType = 3;
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:@"4" forKey:@"OPT"];
    [parameters setObject:@"" forKey:@"body"];
    if ([_mobileTextField.text isPhone]) {
        [parameters setObject:_mobileTextField.text forKey:@"cellPhone"];
    }else {
        [SVProgressHUD showImage:nil status:@"手机号码格式不正确！"];
        return;
    }
    //[parameters setObject:@"1" forKey:@"type"];
    
    if (_requestClient == nil) {
        _requestClient = [[NetWorkClient alloc] init];
        _requestClient.delegate = self;
        
    }
    [_requestClient requestGet:@"app/services" withParameters:parameters];
}

- (void)submitBankInfoRequest {
    if (_isLoading) {
        [SVProgressHUD showImage:nil status:@"正在请求,请稍后再试"];
        return;
    }
    _requestType = 2;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    if (self.editType == 2) {
        [parameters setObject:@"100" forKey:@"OPT"];
        if (_bankId.length > 0) {
            [parameters setObject:self.bankId forKey:@"bankId"];
        }else {
            [SVProgressHUD showImage:nil status:@"银行id有误"];
        }
    }else {
        [parameters setObject:@"99" forKey:@"OPT"];
    }
    [parameters setObject:@"" forKey:@"body"];
    [parameters setObject:AppDelegateInstance.userInfo.userId forKey:@"id"];
    [parameters setObject:_realNameTextField.text forKey:@"realityName"];
    [parameters setObject:_idNumTextfield.text forKey:@"idNumber"];
    [parameters setObject:_bankNumTextField.text forKey:@"addAccount"];
    [parameters setObject:_bankIdStr forKey:@"addBankCode"];
//    [parameters setObject:_provinceIdStr forKey:@"addProviceCode"];
//    [parameters setObject:_cityIdStr forKey:@"addCityCode"];
//    [parameters setObject:_subBranchTextField.text forKey:@"addBranchBankName"];
    [parameters setObject:_mobileTextField.text forKey:@"mobile"];
    [parameters setObject:_verifyNumTextField.text forKey:@"checkCode"];
    
    if (_requestClient == nil) {
        _requestClient = [[NetWorkClient alloc] init];
        _requestClient.delegate = self;
    }
    
    [_requestClient requestGet:@"app/services" withParameters:parameters];
}


- (void)requestData {
    _requestType = 1;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:@"167" forKey:@"OPT"];
    [parameters setObject:@"" forKey:@"body"];
    [parameters setObject:AppDelegateInstance.userInfo.userId forKey:@"id"];
    
    if (_requestClient == nil) {
        _requestClient = [[NetWorkClient alloc] init];
        _requestClient.delegate = self;
    }
    
    [_requestClient requestGet:@"app/services" withParameters:parameters];
}


#pragma - 网络数据回调代理
- (void)startRequest {
    _isLoading = YES;
}

// 返回成功
- (void)httpResponseSuccess:(NetWorkClient *)client dataTask:(NSURLSessionDataTask *)task didSuccessWithObject:(id)obj {
    _isLoading = NO;
    NSDictionary *dics = obj;
    DLOG(@"dic=%@",dics);
    if ([[NSString stringWithFormat:@"%@",[dics objectForKey:@"error"]] isEqualToString:@"-1"]) {
        if (_requestType == 1) {
            if (AppDelegateInstance.userInfo.realName.length > 0) {
                _realNameTextField.text = [NSString stringWithFormat:@"%@",dics[@"realityName"]];
                _idNumTextfield.text = [NSString stringWithFormat:@"%@",dics[@"idNumber"]];
            }
            /*
            if (AppDelegateInstance.userInfo.hasBindCard) {
                NSArray *userBanks = [dics objectForKey:@"userBanks"];
                if (userBanks.count > 0) {
                    _bankNumTextField.userInteractionEnabled = NO;
                    _bankNameTextField.userInteractionEnabled = NO;
                    _locationTextField.userInteractionEnabled = NO;
                    _subBranchTextField.userInteractionEnabled = NO;
                    
                    NSDictionary *dataDict = [dics objectForKey:@"userBanks"][0];
                    if (dataDict) {
                        _bankNumTextField.text = [NSString stringWithFormat:@"%@",dataDict[@"account"]];
                        _bankNameTextField.text = [NSString stringWithFormat:@"%@",dataDict[@"bankName"]];
                        _locationTextField.text = [NSString stringWithFormat:@"%@%@",dataDict[@"province"],dataDict[@"city"]];
                        _subBranchTextField.text = [NSString stringWithFormat:@"%@",dataDict[@"branchBankName"]];
                        //                    _mobileTextField.text = [NSString stringWithFormat:@"%@",dataDict[@"telphone"]];
                        
                        _bankIdStr = [NSString stringWithFormat:@"%@",dataDict[@"bankCode"]];
                        _provinceIdStr = [NSString stringWithFormat:@"%@",dataDict[@"provinceCode"]];
                        _cityIdStr = [NSString stringWithFormat:@"%@",dataDict[@"cityCode"]];
                        
                        [CommonClass buttonEnableWithButton:_nextButtton enable:YES];
                    }

                }
            }*/
            if (self.editType == 2) {
                NSArray *userBanks = [dics objectForKey:@"userBanks"];
                if (userBanks.count) {
                    NSDictionary *dataDict = [dics objectForKey:@"userBanks"][0];
                    if (dataDict) {
                        _bankId = [NSString stringWithFormat:@"%@",dataDict[@"id"]];
                        _realNameTextField.text = [NSString stringWithFormat:@"%@",dics[@"realityName"]];
                        _idNumTextfield.text = [NSString stringWithFormat:@"%@",dics[@"idNumber"]];
                        _bankNumTextField.text = [NSString stringWithFormat:@"%@",dataDict[@"account"]];
                        NSString *bankNameStr = [NSString stringWithFormat:@"%@",dataDict[@"bankName"]];
                        bankNameStr = [CommonClass setBankLogoImageWithBankName:bankNameStr imageView:nil];
                        _bankNameTextField.text = bankNameStr;
                        _locationTextField.text = [NSString stringWithFormat:@"%@%@",dataDict[@"province"],dataDict[@"city"]];
                        _subBranchTextField.text = [NSString stringWithFormat:@"%@",dataDict[@"branchBankName"]];
                        //_mobileTextField.text = [NSString stringWithFormat:@"%@",dataDict[@"telphone"]];
                        
//                        _bankIdStr = [NSString stringWithFormat:@"%@",dataDict[@"bankCode"]];
                        _bankIdStr = [NSString stringWithFormat:@"%@",dataDict[@"bankName"]];
                        _provinceIdStr = [NSString stringWithFormat:@"%@",dataDict[@"provinceCode"]];
                        _cityIdStr = [NSString stringWithFormat:@"%@",dataDict[@"cityCode"]];
                        
                        //[CommonClass buttonEnableWithButton:_nextButtton enable:YES];
                    }
                }
            }
            [_provinceDataArray removeAllObjects];
            [_provinceDataArray addObjectsFromArray:dics[@"provinceCodeNameTable"]];
            
            [_bankDataArray removeAllObjects];
            [_bankDataArray addObjectsFromArray:dics[@"bankCodeNameTable"]];
            
            NSArray *cityArr = [dics objectForKey:@"citylist"];
            for (NSDictionary *item in cityArr) {
                AccountItem *bean = [[AccountItem alloc] init];
                bean.rowId = [NSString stringWithFormat:@"%@",[item objectForKey:@"code"]];
                bean.provinceId = [NSString stringWithFormat:@"%@",[item objectForKey:@"province_code"]];
                bean.name = [NSString stringWithFormat:@"%@",[item objectForKey:@"name"]];
                
                NSMutableDictionary *dics = [_cityDics objectForKey:bean.provinceId];
                if (dics == nil) {
                    dics = [[NSMutableDictionary alloc] init];
                }
                
                [dics setObject:bean forKey:bean.rowId];
                [_cityDics setValue:dics forKey:bean.provinceId];
            }
            
            
            
        }else if (_requestType == 2) {
            
            
            [SVProgressHUD showSuccessWithStatus:[dics objectForKey:@"msg"]];
            AppDelegateInstance.userInfo.hasBindCard = YES;
            AppDelegateInstance.userInfo.realName = _realNameTextField.text;
            if (!AppDelegateInstance.userInfo.isMobileStatus) {
                AppDelegateInstance.userInfo.isMobileStatus = YES;
                AppDelegateInstance.userInfo.mobile = _mobileTextField.text;
            }
            
//            if (self.editType == 1) {
//                AppDelegateInstance.userInfo.hasBindCard = YES;
//                AppDelegateInstance.userInfo.realName = _realNameTextField.text;
//                if (!AppDelegateInstance.userInfo.isMobileStatus) {
//                    AppDelegateInstance.userInfo.isMobileStatus = YES;
//                    AppDelegateInstance.userInfo.mobile = _mobileTextField.text;
//                }
//                [[NSNotificationCenter defaultCenter] postNotificationName:@"addBankCardSuccess" object:nil];
//
//                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"恭喜您，认证成功！" delegate:self cancelButtonTitle:@"点击查看" otherButtonTitles:nil, nil];
//                alertView.tag = 15;
//                [alertView show];
//                
//            }else {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"editBankCardSuccess" object:nil];
                [self.navigationController popViewControllerAnimated:YES];
//            }
            
        }else if (_requestType == 3) {
            [SVProgressHUD showSuccessWithStatus:[dics objectForKey:@"msg"]];
            [self timeDown];
        }
        
        
    }else if ([[NSString stringWithFormat:@"%@",[dics objectForKey:@"error"]] isEqualToString:@"-2"]) {
        
        [ReLogin outTheTimeRelogin:self];
    }else {
        
        [SVProgressHUD showImage:nil status:[dics objectForKey:@"msg"]];
    }
    
}

// 返回失败
- (void)httpResponseFailure:(NetWorkClient *)client dataTask:(NSURLSessionDataTask *)task didFailWithError:(NSError *)error {
    _isLoading = NO;
}

// 无可用的网络
- (void)networkError {
    //  [SVProgressHUD showErrorWithStatus:@"网络不给力，请检查您的网络设置"];
    _isLoading = NO;
}

- (void)timeDown {
    __block NSInteger timeout = DOWNTIME; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 1 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                
                [_verifyNumButton setTitle:@"重新获取" forState:UIControlStateNormal];
                _verifyNumButton.userInteractionEnabled = YES;
                [_verifyNumButton setAlpha:1];
            });
        }else{
            NSInteger seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2ld", (long)seconds];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                
                [_verifyNumButton setTitle:[NSString stringWithFormat:@"%@秒",strTime] forState:UIControlStateNormal];
                _verifyNumButton.userInteractionEnabled = NO;
                [_verifyNumButton setAlpha:0.4];
            });
            timeout--;
        }
    });
    dispatch_resume(timer);
}

#pragma mark - Picker
//返回显示的列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (_selectViewType == 1) {
        return 1;
    }else {
        return 2;
    }
}
//返回当前列显示的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (_selectViewType == 1) {
        return _bankDataArray.count;
    }else {
        NSInteger num = 0;
        
        if (component == 0) {//省份个数
            num = [_provinceDataArray count];
        } else {//市的个数
            num  = [_citysArr count];
        }
        return num;
    }
}

//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (_selectViewType == 1) {
        return [_bankDataArray objectAtIndex:row][@"name"];
    }else {
        NSString *contentStr = @"";
        if (component == 0) {//选择省份名
            contentStr =  [_provinceDataArray objectAtIndex:row][@"name"];
            
        } else {//选择市名
            contentStr =  [_citysArr objectAtIndex:row];
        }
        return contentStr;
    }
}

//监听轮子的移动
- (void)pickerView:(UIPickerView *)pickerView
      didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (_selectViewType == 1) {
        _bankNameStr = [_bankDataArray objectAtIndex:row][@"name"];
        _holderBankIdStr = [NSString stringWithFormat:@"%@",_bankDataArray[row][@"code"]];
    }else {
        if (component == 0) {
            NSString *seletedProvince = [_provinceDataArray objectAtIndex:row][@"name"];
            _holderProvIdStr = [NSString stringWithFormat:@"%@",_provinceDataArray[row][@"code"]];
            
            //         [self requestCityData:_provinceIdArr[row]];
            
            //        //重点！更新第二个轮子的数据
            //        [self.pickerView reloadComponent:1];
            
            NSInteger selectedCityIndex = [self.pickerView selectedRowInComponent:1];
            NSString *seletedCity = @"";
            _cityNameArr = [[_cityDics objectForKey:[NSString stringWithFormat:@"%@",_provinceDataArray[row][@"code"]]] allValues];
            if (_cityNameArr.count) {
                [_citysArr removeAllObjects];
                [_cityIdArr removeAllObjects];
                for (AccountItem *cityItem in _cityNameArr) {
                    [_citysArr addObject:cityItem.name];
                    [_cityIdArr addObject:cityItem.rowId];
                }
                [_pickerView reloadComponent:1];
                if (selectedCityIndex > _citysArr.count) {
                    
                    seletedCity = [_citysArr objectAtIndex:_citysArr.count -1];
                    _holderCityIdStr = _cityIdArr[_citysArr.count -1];
                    
                }else{
                    seletedCity = [_citysArr objectAtIndex:selectedCityIndex];
                    _holderCityIdStr = _cityIdArr[selectedCityIndex];
                }
                
            }
            _provinceStr = [NSString stringWithFormat:@"%@,%@", seletedProvince,seletedCity];
        }else {
            NSInteger selectedProvinceIndex = [self.pickerView selectedRowInComponent:0];
            NSString *seletedProvince = [_provinceDataArray objectAtIndex:selectedProvinceIndex][@"name"];
            NSString *seletedCity = @"";
            seletedCity = [_citysArr objectAtIndex:row];
            _holderCityIdStr = [_cityIdArr objectAtIndex:row];
            _provinceStr = [NSString stringWithFormat:@"%@,%@", seletedProvince,seletedCity];
           // DLOG(@"%@",_provinceStr);
        }
    }
}

#pragma mark - View
- (void)initView {
    _scrollView.scrollEnabled = YES;
    _scrollView.contentSize = CGSizeMake(0, GetMaxY(_nextButtton) + 20);
    
    if (AppDelegateInstance.userInfo.isMobileStatus) {
        _mobileTextField.text = AppDelegateInstance.userInfo.mobile;
        _mobileTextField.userInteractionEnabled = NO;
    }
    if (AppDelegateInstance.userInfo.realName.length > 0) {
        _realNameTextField.userInteractionEnabled = NO;
        _idNumTextfield.userInteractionEnabled = NO;
    }
//    if (AppDelegateInstance.userInfo.hasBindCard) {
//        _bankNumTextField.userInteractionEnabled = NO;
//        _bankNameTextField.userInteractionEnabled = NO;
//        _locationTextField.userInteractionEnabled = NO;
//        _subBranchTextField.userInteractionEnabled = NO;
//    }
    
    [_headerImageView sd_setImageWithURL:[NSURL URLWithString:AppDelegateInstance.userInfo.userImg] placeholderImage:KDefaultHead];
    _usernameLabel.text = [CommonClass getAsteriskUserName];
   
    _usernameLabel.font = KFontSize(14);
    _headerOneValue.font = KFontSize(14);
    _headerTwoValue.font = KFontSize(14);
    _headerThreeValue.font = KFontSize(14);
    _realNameValue.font = KFontSize(14);
    _realNameTextField.font = KFontSize(14);
    _idNumValue.font = KFontSize(14);
    _idNumTextfield.font = KFontSize(14);
    _bankNameValue.font = KFontSize(14);
    _bankNameTextField.font = KFontSize(14);
    _locationValue.font = KFontSize(14);
    _locationTextField.font = KFontSize(14);
    _subBranchValue.font = KFontSize(14);
    _subBranchTextField.font = KFontSize(14);
    _bankNumValue.font = KFontSize(14);
    _bankNumTextField.font = KFontSize(14);
    _mobileValue.font = KFontSize(14);
    _mobileTextField.font = KFontSize(14);
    _verifyNumValue.font = KFontSize(14);
    _verifyNumTextField.font = KFontSize(14);
    _verifyNumButton.titleLabel.font = KFontSize(13);
    _nextButtton.titleLabel.font = KFontSize(16);
    _nextButtton.clipsToBounds = YES;
    _nextButtton.layer.cornerRadius = KAdaptNum(5.0);
    [CommonClass buttonEnableWithButton:_nextButtton enable:NO];
    
    _realNameTextField.delegate = self;
    _idNumTextfield.delegate = self;
    _bankNameTextField.delegate = self;
    _bankNumTextField.delegate = self;
    _subBranchTextField.delegate = self;
    _locationTextField.delegate = self;
    _mobileTextField.delegate = self;
    
    [_realNameTextField addTarget:self action:@selector(changeValue) forControlEvents:UIControlEventEditingChanged];
    [_idNumTextfield addTarget:self action:@selector(changeValue) forControlEvents:UIControlEventEditingChanged];
    [_bankNameTextField addTarget:self action:@selector(changeValue) forControlEvents:UIControlEventEditingChanged];
    [_bankNumTextField addTarget:self action:@selector(changeValue) forControlEvents:UIControlEventEditingChanged];
    [_subBranchTextField addTarget:self action:@selector(changeValue) forControlEvents:UIControlEventEditingChanged];
    [_locationTextField addTarget:self action:@selector(changeValue) forControlEvents:UIControlEventEditingChanged];
    [_mobileTextField addTarget:self action:@selector(changeValue) forControlEvents:UIControlEventEditingChanged];
    [_verifyNumTextField addTarget:self action:@selector(changeValue) forControlEvents:UIControlEventEditingChanged];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    _scrollView.contentSize = CGSizeMake(MSWIDTH, CGRectGetMaxY(_nextButtton.frame) + 20);
}

- (void)initSelectView {
    //底部筛选视图
    _provinceBackView = [[UIView alloc] initWithFrame:CGRectMake(0, MSHIGHT+kSelectViewHeight, MSWIDTH, kSelectViewHeight)];
    _provinceBackView.backgroundColor  = [UIColor blackColor];
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, MSWIDTH, GetHeight(_provinceBackView) - 40)];
    //    指定Delegate
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    _pickerView.backgroundColor = [UIColor whiteColor];
    //    显示选中框
    _pickerView.showsSelectionIndicator=YES;
    [_provinceBackView addSubview:_pickerView];
    [self.view addSubview:_provinceBackView];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(10, 5, 60, 30);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [_provinceBackView addSubview:cancelBtn];
    
    UIButton *surebtn = [UIButton buttonWithType:UIButtonTypeCustom];
    surebtn.frame = CGRectMake(MSWIDTH - 70, 5, 60, 30);
    [surebtn setTitle:@"确定" forState:UIControlStateNormal];
    [surebtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
    [_provinceBackView addSubview:surebtn];
}


- (void)initAlertView {
    _alertView = [UIView viewWithFrame:RECTMAKE(0, 0, MSWIDTH, MSHIGHT) backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.3] superview:AppDelegateInstance.window];
    _alertView.hidden = YES;
    
    _alertBackView = [UIView viewWithFrame:RECTMAKE(30, KAdaptNum(100), MSWIDTH - 60, MSWIDTH - 60) backgroundColor:[UIColor whiteColor] superview:_alertView];
    _alertBackView.layer.cornerRadius = KAdaptNum(5.0);
    
   UIImageView *alertImageView = [UIImageView imageViewWithFrame:RECTMAKE((GetWidth(_alertBackView) - KAdaptNum(47))/2, 20, KAdaptNum(47), KAdaptNum(47)) imageFile:@"AddBank_Alert" superview:_alertBackView];
    
    UILabel *label = [UILabel labelWithFrame:RECTMAKE(10, GetMaxY(alertImageView) + 20, GetWidth(_alertBackView) - 20, KAdaptNum(40)) text:@"请您在新打开的页面上完成绑定银行卡" textColor:KBlackColor textFont:14 fitWidth:NO superview:_alertBackView];
    label.numberOfLines = 0;
    
    
    UILabel *label1 = [UILabel labelWithFrame:RECTMAKE(10, GetMaxY(label) + 10, GetWidth(_alertBackView) - 20, KAdaptNum(20)) text:@"未完成绑定前请不要关闭此窗口" textColor:KColor textFont:12 fitWidth:NO superview:_alertBackView];
    label1.numberOfLines = 0;
    
    ClickButton *failureButton = [[ClickButton alloc] initWithFrame:RECTMAKE(15, GetMaxY(label1) + 20, (GetWidth(_alertBackView) - 45)/2, KAdaptNum(40))];
    [failureButton setTitle:@"绑定失败？" forState:UIControlStateNormal];
    [failureButton addTarget:self action:@selector(failureButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_alertBackView addSubview:failureButton];
    failureButton.alpha = 0.8;
    
    ClickButton *successButton = [[ClickButton alloc] initWithFrame:RECTMAKE(GetMaxX(failureButton) + 15, GetMinY(failureButton), (GetWidth(_alertBackView) - 45)/2, KAdaptNum(40))];
    [successButton setTitle:@"绑定成功" forState:UIControlStateNormal];
    [successButton addTarget:self action:@selector(successButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_alertBackView addSubview:successButton];
    
    _alertBackView.frame = RECTMAKE(30, KAdaptNum(100), MSWIDTH - 60, GetMaxY(successButton)+20);
}

- (void)rightBarButtonClick {
    payLimitViewController *limitTable = [[payLimitViewController alloc] init];
    limitTable.titleTag = 1;
    [self.navigationController pushViewController:limitTable animated:YES];
}
- (RealNameAuthView *)realNameView{
    if (_realNameView == nil) {
        _realNameView = [[RealNameAuthView alloc] init];
       
    }
    return _realNameView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
